home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / WINDEV.ARC / WINVUENT.C < prev    next >
C/C++ Source or Header  |  1989-04-23  |  6KB  |  212 lines

  1. /* 
  2.  * Winvue initialization module
  3.  *
  4.  * Written by Bill Hall
  5.  * 3665  Benton Street, #66
  6.  * Santa Clara, CA 95051
  7.  *
  8.  */
  9.  
  10. #define NOCOMM        
  11. #define NOKANJI
  12. #define NOATOM
  13. #define NOBITMAP
  14. #define NOMINMAX
  15. #include <windows.h>
  16. #include <stdlib.h>
  17. #include <ttycls.h>
  18. #include <ascii.h>
  19. #include "winvue.h"
  20.  
  21. /* local function declarations */
  22. static BOOL NEAR RegisterWindowClass(HANDLE);
  23. static BOOL NEAR MakeAndShowMainWnd(HANDLE, int);
  24. static int NEAR OpenCmdlineFile(LPSTR lptr);
  25. static void NEAR GetPrevInstanceData(HANDLE);
  26.  
  27. /* enter here to initialize the program */
  28. BOOL FAR InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow)
  29. HANDLE hInstance, hPrevInstance;
  30. LPSTR lpszCmdLine;
  31. int cmdShow;
  32. {
  33.  
  34.   /* set a pointer to the appropriate message function */
  35.    DoMessage = DoGetMessage;
  36.  
  37.   /* if first instance, do these things */
  38.     if (hPrevInstance == NULL) {
  39.       /* get icon string */
  40.         LoadString(hInstance,IDS_ICONSTRING,(LPSTR)szIconTitle,
  41.                 sizeof(szIconTitle));
  42.       /* register window class */
  43.     if (!RegisterWindowClass(hInstance))
  44.         return FALSE;
  45.       /* load the menu accelerators */
  46.     hAccel = LoadAccelerators(hInstance, (LPSTR)szAppName);
  47.     }
  48.  
  49.   /* otherwise, just load data normally obtained during registration */
  50.     else
  51.     GetPrevInstanceData(hPrevInstance);
  52.  
  53.   /* create the window */
  54.     if (!MakeAndShowMainWnd(hInstance,cmdShow))
  55.     return FALSE;
  56.  
  57.   /* see if a file was specified on the command line */
  58.     hFile = OpenCmdlineFile(lpszCmdLine);
  59.  
  60.   /* show success */
  61.     return TRUE;
  62. }
  63.  
  64. /* register the windows class */
  65. static BOOL near RegisterWindowClass(hInstance)
  66. HANDLE hInstance;
  67. {
  68.  
  69.     PWNDCLASS pWndClass;
  70.     HANDLE hTemp;
  71.  
  72.   /* get the application name from the resources */
  73.     LoadString(hInstance,IDS_APPNAME,(LPSTR)szAppName,sizeof(szAppName));
  74.  
  75.   /* make space for the WNDCLASS structure in the heap */
  76.     hTemp = LocalAlloc(LPTR,sizeof(WNDCLASS));
  77.     pWndClass = (PWNDCLASS)LocalLock(hTemp);
  78.     
  79.   /* enter data */
  80.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);    /* stock cursor */
  81.     pWndClass->hIcon = NULL;                /* no icon */
  82.     pWndClass->lpszMenuName = (LPSTR)szAppName;        /* this menu */
  83.     pWndClass->lpszClassName = (LPSTR)szAppName;    /* class name */
  84.     pWndClass->hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); /* background */
  85.     pWndClass->hInstance = hInstance;            /* instance */
  86.     pWndClass->style = CS_VREDRAW | CS_HREDRAW;        /* style */
  87.     pWndClass->lpfnWndProc = MainWndProc;    /* pointer to wind proc */
  88.     pWndClass->cbWndExtra = sizeof(PTTYWND);   /* space for ptr to MWnd */
  89.  
  90.   /* if failure, return FALSE */
  91.     if (!RegisterClass((LPWNDCLASS)pWndClass))
  92.     return FALSE;
  93.  
  94.   /* success, so free up heap data and show ok */
  95.     LocalUnlock(hTemp);
  96.     LocalFree(hTemp);
  97.     return TRUE;
  98. }
  99.  
  100. /* if we don't need to register the window, we have to load these strings */
  101. static void NEAR GetPrevInstanceData(HANDLE hPrevInstance)
  102. {
  103.  
  104.     GetInstanceData(hPrevInstance, (PSTR)szAppName, sizeof(szAppName));
  105.     GetInstanceData(hPrevInstance, (PSTR)szIconTitle, sizeof(szIconTitle));
  106.     GetInstanceData(hPrevInstance, (PSTR)&hAccel, sizeof(hAccel));
  107. }
  108.  
  109. /* create a window of the class registered above */
  110. static BOOL NEAR MakeAndShowMainWnd(hInstance, cmdShow)
  111. HANDLE hInstance;
  112. int cmdShow;
  113. {
  114.  
  115.   /* get the text for the title */
  116.     LoadString(hInstance, IDS_WINTITLE, (LPSTR)szWinTitle,sizeof(szWinTitle));
  117.  
  118.   /* create the window */
  119.     MWnd.hWnd = CreateWindow((LPSTR)szAppName,    /* class */
  120.                  (LPSTR)szWinTitle,    /* title string */
  121.                  WS_OVERLAPPEDWINDOW,    /* style */
  122.                  CW_USEDEFAULT,0,
  123.                  CW_USEDEFAULT,0,
  124.                  (HWND)NULL,    /* no parent */
  125.                  (HMENU)NULL,    /* class menu */
  126.                  (HANDLE)hInstance,    /* instance */
  127.                  (LPSTR)&MWnd);    /* ptr to window structure */
  128.  
  129.   /* if successful, display the window */
  130.     if (MWnd.hWnd && MWnd.hVidBuf) {
  131.     ShowWindow(MWnd.hWnd, cmdShow);
  132.         UpdateWindow(MWnd.hWnd);
  133.     return TRUE;
  134.     }
  135.   /* return failure */
  136.     return FALSE;
  137. }
  138.  
  139. /* read the command line and open a file, if any */
  140. static int NEAR OpenCmdlineFile(LPSTR cmdline)
  141. {
  142.  
  143.     char ch;
  144.     int i, len;
  145.     HANDLE hname;
  146.     char *pname;
  147.     LPSTR lptr;
  148.     int hfile = 0;
  149.  
  150.     lptr = cmdline;
  151.     len = 0;
  152.  
  153.   /* isolate first field in command line */
  154.     while (ch = *lptr++)
  155.     if (ch == ' ')
  156.         break;
  157.     else
  158.         len++;
  159.  
  160.     if (len) {
  161.       /* make space for file name in heap */
  162.     if (hname = LocalAlloc(LPTR, len + 1)) {
  163.         pname = LocalLock(hname);
  164.       /* copy the name */
  165.         for(i = 0; i < len; i++)
  166.         *(pname + i) = *cmdline++;
  167.         *(pname + len) = NUL;
  168.       /* try opening the file */
  169.         hfile = OpenTheFile(pname);        
  170.       /* discard the heap allocation */
  171.         LocalUnlock(hname);
  172.         LocalFree(hname);
  173.     }
  174.     }
  175.     return hfile;
  176. }
  177.  
  178. /* this routine is called upon receipt of the WM_CREATE message */
  179. void WndCreate(hWnd, lParam)
  180. HWND hWnd;
  181. LONG lParam;
  182. {
  183.  
  184.     HDC hDC;
  185.     TEXTMETRIC TM;
  186.     short width, height, cwidth, cheight;
  187.     LPCREATESTRUCT pCS;
  188.     PTTYWND pMWnd;
  189.     
  190.   /* we are going to create a buffer for a window this large */
  191.     width = GetSystemMetrics(SM_CXFULLSCREEN);
  192.     height = GetSystemMetrics(SM_CYFULLSCREEN);
  193.  
  194.   /* get the font size */
  195.     hDC = GetDC(hWnd);
  196.     GetTextMetrics(hDC, &TM);
  197.     cwidth = TM.tmAveCharWidth;
  198.     cheight = TM.tmHeight + TM.tmExternalLeading;
  199.     ReleaseDC(hWnd, hDC);
  200.  
  201.   /* here we retrieve the pointer to our local window stucture */
  202.     pCS = (LPCREATESTRUCT)lParam;
  203.     pMWnd = (PTTYWND)LOWORD(pCS->lpCreateParams);
  204.  
  205.   /* initialize the extra data to the pointer to the TWnd structure */
  206.     SetWindowWord(hWnd,0,(WORD)pMWnd);
  207.  
  208.   /* now create the text buffer and set some defaults */
  209.     InitTTYWindow(pMWnd,0,0,width,height,cwidth,cheight,
  210.                 FALSE,FALSE,TRUE,0,0xff);
  211. }
  212.